%% Summarize beta parameters

clear
cd ../china/data
load hs10_indlist.raw;
indN=length(hs10_indlist);

industry=0;
beta_full=zeros(1,4);
importers=0;
exporters=0;

sample_select=zeros(51,1);
for y=1:50
if (y>1 & y<5)
sample_select(y,1)=1;
else
if (y>5 & y<43)
sample_select(y,1)=1;
end
end
end

for w=1:50 

if sample_select(w,1)==1

folder=sprintf('../china/regular/raw_%d',hs10_indlist(w,1))


cd (folder)


load imp.raw;
load data2_q_regular.mat
load beta_newq_regular.mat;
load EV_newq.mat

else

folder=sprintf('../china/biggest/raw_%d',hs10_indlist(w,1))


cd (folder)


load imp.raw;
load data2_q_biggest.mat
load beta_newq_biggest.mat;
load EV_newq.mat

end

M=length(imp);

beta_full=[beta_full; beta_p_sol beta_x_sol beta_c_sol xi_tilde_sol];
industry=[industry; hs10_indlist(w,1)];
importers=[importers; M];
exporters=[exporters; X];

end

industry=industry(2:end,:);
beta_full=beta_full(2:end,:);
importers=importers(2:end,:);
exporters=exporters(2:end,:);


summary=[importers exporters];

load ../china/data/trade_share.raw; 
trade_share=trade_share(1:w,:);
trade_share=trade_share/sum(trade_share);
trade_share=repmat(trade_share,1,4);
beta_full_weighted=beta_full.*trade_share;
beta_full_weighted=sum(beta_full_weighted,1);
b1=mean(beta_full)
b2=median(beta_full)
b3=beta_full_weighted

beta_summary=[b1; b2; b3];

cd ../china
xlswrite('beta_summary',beta_full)
